Unconditional Control Statements

Statements that transfers control from on part of the program to another part unconditionally Different unconditional statements are

  1. goto
  2. break
  3. continue

1. goto Statement

The goto statement is used for unconditional branching or transfer of the program execution to the labeled statement. The goto statement is strongly discouraged as it makes it difficult to follow the program logic, this way inducing to errors. In some (mostly rare) cases the goto statement allows to write uncluttered code, for example, when handling multiple exit points leading to the cleanup code at a function exit (and exception handling is not a better option). Except in those rare cases, the use of unconditional jumps is a frequent symptom of a complicated design, as the presence of many levels of nested statements.

Syntax:

label:
    statement(s);
goto label;
statement(s);
goto label;
      statement(s);
      statement(s);
label:

The goto statement to branch unconditionally from one point to another in the program. The goto requires a label in order to identify the place where the branch is to be made. A Label is any valid identifier and must be followed by a colon. The label is placed immediately before the statement where the control is to be transferred. The general form of goto is shown in above code. Depends on the placement of label in the program jump is defined in two ways. One is forward jump and another is backward jump.

Forward Jump: in this jump label is placed after the goto statement. some statements will be skipped while execution of code.

Backward Jump: in this jump label is placed before the goto statement. some statements will be executed many times as this jump is creating unconditional iterations.

Example: A goto can, for example, be used to break out of two nested loops. This example breaks after replacing the first encountered non-zero element with zero.

for (int i = 0; i < 30; ++i) 
{
   for (int j = 0; j < 30; ++j) 
   {
      if (a[i][j] != 0) 
      {
         a[i][j] = 0;
         goto done;
      }
   } 
}
done:

The goto statement should almost always be avoided, there are rare cases when it enhances the readability of code. One such case is an “error section”.

2. break statement

when a break statement is encountered within a loop ,loop is immediately exited and the program continues with the statements immediately following loop.

Be sure to use break commands unless you want multiple conditions to have the same action. Otherwise, it will “fall through” to the next set of commands. break can only break out of the innermost level. If for example you are inside a switch and need to break out of a enclosing for loop you might well consider adding a boolean as a flag, and check the flag after the switch block instead of the alternatives available. (Though even then, refactoring the code into a separate function and returning from that function might be cleaner depending on the situation, and with inline functions and/or smart compilers there need not be any runtime overhead from doing so.)

Syntax:

break;

Example:

/*c program to find sum of n natural numbers */
#include<stdio.h>
int main()
{
       int i ,sum = 0,n;
       cout<<”Enter N";
       cin>>n;
       i=1;
       L1:
           sum = sum + i;
           i++;
           if(i>n) 
             break;
       goto L1;
       cout<<”Sum of first”<<n<<”natural numbers is:
”<<sum; 
       return 0;
}

3. Continue Statement

It is used to continue the iteration of the loop statement by skipping the statements after continue statement. It causes the control to go directly to the test condition and then to continue the loop.

Syntax:

continue;

Example:

/*c program to find sum of n positive numbers read from keyboard*/
#include<stdio.h>
int main()
{
int i ,sum = 0,n,number;
cout<<Enter N";
cin>>n;
for(i=1;i<=n;i++)
{
    cout<<“Enter a number:”;
    cin>>number;
    if(number<0) 
        continue;
    sum = sum + number;
}
cout<<“Sum of”<<n<<” numbers is:”<<sum;
return 0;
}

continue is not relevant to switch block. Calling continue within a switch block will lead to the “continue” of the loop which wraps the switch block.

Related posts